import type { Module } from 'vuex'
import type { IRootState } from '@/store/types'
import type { ISystemState } from './types'

import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData
} from '@/service/main/system/system'

const pageUrlMap: any = {
  users: '/users/list',
  role: '/role/list',
  goods: '/goods/list',
  menu: '/menu/list',
  department: '/department/list'
}

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
      departmentList: [],
      departmentCount: 0
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  mutations: {
    changeUsersList(state, usersList: any[]) {
      state.usersList = usersList
    },
    changeUsersCount(state, usersCount: number) {
      state.usersCount = usersCount
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount
    },
    changeDepartmentList(state, departmentList: any[]) {
      state.departmentList = departmentList
    },
    changeDepartmentCount(state, departmentCount: number) {
      state.departmentCount = departmentCount
    }
  },
  actions: {
    /**
     * 获取数据
     * @param param0
     * @param payload
     */
    async getPageListAction({ commit }, payload: any) {
      // 处理参数
      const pageName = payload.pageName
      const pageUrl = pageUrlMap[pageName]
      // 发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      // 处理结果
      const { list, totalCount } = pageResult.data

      commit(`change${pageName.charAt(0).toUpperCase() + pageName.slice(1)}List`, list)
      commit(`change${pageName.charAt(0).toUpperCase() + pageName.slice(1)}Count`, totalCount)
    },

    /**
     * 删除数据
     * @param param0
     * @param payload
     */
    async deletePageDataAction({ dispatch }, payload: any) {
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl)

      // 重新请求
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },

    /**
     * 新建数据
     * @param param0
     * @param payload
     */
    async createPageDataAction({ dispatch }, payload: any) {
      // 创建
      const { pageName, newData } = payload
      const url = `/${pageName}`
      await createPageData(url, newData)
      // 重新请求
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },

    /**
     * 编辑数据
     * @param param0
     * @param payload
     */
    async editPageDataAction({ dispatch }, payload: any) {
      // 创建
      const { pageName, editData, id } = payload
      const url = `/${pageName}/${id}`

      await editPageData(url, editData)
      // 重新请求
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}

export default systemModule
